package com.example.ssm;

import com.example.ssm.Bean.Account;
import com.example.ssm.Dao.AccountMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * @create 2022-03-2022/3/6-9:41
 */
public class test {
    private InputStream is =null;
    private SqlSessionFactoryBuilder builder=null;
    private SqlSessionFactory factory=null;
    private SqlSession sqlSession=null;
    private AccountMapper accountMapper=null;

    @Before
    public void   conn() throws IOException {
        is = Resources.getResourceAsStream("SqlMapperConfig.xml");
        // 2. 创建 SqlSessionFactory 工厂
        builder = new SqlSessionFactoryBuilder();
        factory = builder.build(is);
        // 3. 获取 SqlSession 对象
        sqlSession = factory.openSession();
        // 4. 使用 SqlSession 创建 Mapper 的代理对象
        accountMapper=sqlSession.getMapper(AccountMapper.class);
    }

    @After
    public void close() throws IOException {
        sqlSession.commit();
        sqlSession.close();
        is.close();
    }

    @Test
    public void testfindAll(){
        List<Account> list =accountMapper.findAll();
        for(Account account:list){
            System.out.println(account);
        }
    }

    @Test
    public void testsave(){
        Account account=new Account();
        account.setName("LaoBa");
        account.setMoney(9999);
        accountMapper.saveAccount(account);
    }
}
